package com.dmcb.trade.business.crawlers.article;

import com.dmcb.common.business.utils.DateUtil;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 百家号
 * Created by wangyong on 2017/9/21.
 */
@Service
public class BaiJiaHao2Parser  extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.BAIJIAHAO2_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "div.article-title>h2";
    }

    /**
     * 获取日期选择器
     *
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "div.author-txt>div.article-source";
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "div.article-content";
    }

    /**
     * 获取封面选择器
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "div.article-content>div img[src]";
    }

    /**
     * 获取作者选择器
     *
     * @return 作者选择器
     */
    @Override
    protected String authorNameSelect() {
        return "p.author-name";
    }

    @Override
    protected void setPostDate(Document doc, Article article) {
        String time = doc.select(dateSelect()).text();
        if(StringUtils.isBlank(time)){
            return;
        }
        time = time.replace("百家号","").trim();
        time = time.contains("最")?time.substring(0,time.indexOf("最")) : time;
        Date date = DateUtil.parseDate(time);
        article.setPostTime(date);
    }

    @Override
    protected String platform() {
        return "百家号";
    }
}
